package com.zy.edu.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zy.edu.bean.*;
import com.zy.edu.bean.ToolInfo;
import com.zy.edu.config.SystemParam;
import com.zy.edu.dao.ToolInfoDao;
import com.zy.edu.service.ClassinfoService;
import com.zy.edu.service.ToolInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zy.edu.util.FileUpload;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HuangHaiShui
 * @since 2021-08-24
 */
@Service
@Transactional
public class ToolInfoServiceImpl extends ServiceImpl<ToolInfoDao, ToolInfo> implements ToolInfoService {

    @Resource
    ClassinfoService classinfoService;
    @Override
    public Map saveOrUpdate(ToolInfo toolInfo) {
        List<ToolInfo> tempList = this.baseMapper.selectList(new EntityWrapper<ToolInfo>()
                .eq("name", toolInfo.getName()));
        if(StringUtils.isEmpty(toolInfo.getId())){
            toolInfo.setCreateTime(new Date());
            if(tempList.size()>0){
                return ResponseInfo.error("名称已存在，不能创建");
            }else {
                this.baseMapper.insert(toolInfo);
            }
        }else{
            // 若存在名称相同的，且不是自己，则提示重复
            if(tempList.size()>0 && !tempList.get(0).getId().equals(toolInfo.getId())){
                return ResponseInfo.error("名称已存在，不能修改");
            }else {
                this.baseMapper.updateById(toolInfo);
            }
        }
        return ResponseInfo.success(null);
    }

    @Override
    public Map list(ToolInfo toolInfo, InputParam inputParam) {
        EntityWrapper<ToolInfo> entityWrapper = new EntityWrapper<ToolInfo>();
        if(!StringUtils.isEmpty(toolInfo.getName())){
            entityWrapper.like("name",toolInfo.getName());
        }
        entityWrapper.orderBy("create_time",false);

        //查分页
        if(inputParam!=null && inputParam.getPage()!=null && inputParam.getSize()!=null
                && inputParam.getPage()>0 &&  inputParam.getSize()>0){
            Page<ToolInfo> page = new Page<ToolInfo>(inputParam.getPage(), inputParam.getSize());
            List<ToolInfo> tempList = this.baseMapper.selectPage(page,entityWrapper);
            Integer total = this.baseMapper.selectCount(entityWrapper);

            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("rows", tempList);
            resultMap.put("total", total);
            return ResponseInfo.success(resultMap);
        }else {//查全部
            List<ToolInfo> tempList = this.baseMapper.selectList(entityWrapper);
            return ResponseInfo.success(tempList);
        }

    }

    @Override
    public Map dele(ToolInfo toolInfo) {
        if(!StringUtils.isEmpty(toolInfo.getId())){
            this.baseMapper.deleteById(toolInfo.getId());
        }
        return ResponseInfo.success(null);
    }



    @Override
    public Map getById(ToolInfo toolInfo, InputParam inputParam) {
        if(StringUtils.isEmpty(toolInfo.getId())) {
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }
        return ResponseInfo.success(this.selectById(toolInfo.getId()));
    }

    @Value("${upload.folderName}")
    private String folderName;
    @Override
    public Map photo(MultipartFile file, InputParam inputParam) throws IOException {
        if(inputParam.getToolId()==null) {
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }else {
            String filename = FileUpload.writeUploadFile(file, folderName, "tool");
            //TODO:每次上传前，先删除旧图片
            ToolInfo toolInfo = new ToolInfo();
            toolInfo.setId(inputParam.getToolId());
            toolInfo.setCoverUrl(filename);
            this.baseMapper.updateById(toolInfo);
            return ResponseInfo.success(filename);
        }
    }
}
